package com.zxbit.flash.presenter;

import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.IdentityAuthMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.AuthInfoResponse;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.response.CompareResultResponse;
import com.zxbit.flash.bean.IdentityData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class IdentityAuthPresenter extends BasePresenter {
    private IdentityAuthMvpView mvpView;

    public IdentityAuthPresenter(IdentityAuthMvpView mvpView) {
        this.mvpView = mvpView;
    }

    /**
     * 获取认证信息
     */
    public void getIdentityInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("userUuid", UserInfo.getShowUuid());
        List<String> authPageCode = new ArrayList<>();
        authPageCode.add("identity_code");
        map.put("authPageCode", authPageCode);
        toSubScribe(req.getAuthInfoData(UserInfo.getToken(), ParamsFactory.getCommonParams(map)), new CustomConsumer<AuthInfoResponse>() {
            @Override
            public void call(AuthInfoResponse authInfoResponse) {
                mvpView.onGetDataNext(authInfoResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 保存用户信息
     * @param identity
     * @param imageInfoParams
     */
    public void saveIdentity(IdentityData.IdentityBean identity, List<IdentityData.ImageInfoBean> imageInfoParams) {
        Map<String, Object> fromMap = identity.toMap();
        fromMap.put("authPageCode", "identity_code");
        fromMap.put("userUuid", UserInfo.getSaveUuid());
        fromMap.put("imageInfoParams", imageInfoParams);
        if (UserInfo.isUserInformationWrong())
            fromMap.put("isInformationWrong", 1);

        toSubScribe(req.saveIdentity(UserInfo.getToken(), ParamsFactory.getCommonParams(fromMap)), new CustomConsumer<BlankResponse>() {
            @Override
            public void call(BlankResponse blankResponse) {
                mvpView.onGetDataNext(blankResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });

    }


    /**
     * 照片对比的
     * @param firstImagePath
     * @param secondImagePath
     */
    public void imgCompare(String firstImagePath, String secondImagePath) {
        Map<String, Object> map = new HashMap<>();
        map.put("userUuid", UserInfo.getSaveUuid());
        map.put("firstImagePath", firstImagePath);
        map.put("secondImagePath", secondImagePath);
        toSubScribe(req.imgCompare(UserInfo.getToken(), ParamsFactory.getCommonParams(map)), new CustomConsumer<CompareResultResponse>() {
            @Override
            public void call(CompareResultResponse compareResultResponse) {
                mvpView.onGetDataNext(compareResultResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }
}
